package com.online.user.controller;

import com.github.pagehelper.Page;
import com.online.entity.PageResult;
import com.online.entity.Result;
import com.online.pojo.User;
import com.online.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 
 */
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService iUserService;


    /**
    * 查询全部
    * @param
    * @return
    */
    @GetMapping("/selectAll")
    public Result< List<User> > selectAll(){
        return Result.ok("查询成功",iUserService.selectAll());
    }


    /**
     * 条件查询
     * @param iUser
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<User> > select(@RequestBody User iUser){
        return Result.ok("条件查询成功",iUserService.selectList(iUser));
    }

    /**
     * 新增
     * @param iUser
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody User iUser){
        iUserService.insert(iUser);
        return Result.ok("新增成功");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        iUserService.delete(ids);
        return Result.ok("删除成功");
    }


    /**
     * 修改
     * @param iUser
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody User iUser){
        iUserService.update(iUser);
        return Result.ok("修改成功");
    }

    /**
    * 分页搜索实现
    * @param page 默认值1
    * @param size 默认值10
    * @return
    */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
    Page<User> pageList = iUserService.pageAll(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
    * 分页条件搜索实现
    * @param iUser
    * @param page
    * @param size
    * @return
    */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody User iUser,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
    Page<User> pageList = iUserService.pageList(iUser,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }
}




